<?php
// +----------------------------------------------------------------------
// | YFCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://www.rainfer.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: rainfer <81818832@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\controller\Common;
use app\admin\model\User as UserModel;
use think\Db;
use think\Session;

class Login extends Common 
{
    protected function _initialize()
    {
        parent::_initialize();
    }
    
    /**
     * 登录显示
     */
    public function login()
    {
        if($this->check_admin_login()) $this->redirect('admin/Index/index');
        $userinfo = session('userinfo');
        $this->assign('$userinfo',$userinfo);

        return $this->fetch();
    }
    
    
    /**
     * 登录验证
     */
    public function runlogin()
    {
        if (!request()->isAjax()){
            $this->error("提交方式错误！",url('admin/Login/login'));
        }else{
            
            $user_name=input('user_name');
            $password=input('password');
            $rememberme=input('rememberme');
            $doginfo = input('doginfo');
            $admin=new UserModel;
            
            if($admin->login($user_name,$password,$doginfo,$rememberme)){
                $this->success('恭喜您，登陆成功',url('admin/Index/index'));
            }else{
                $this->error($admin->getError(),url('admin/Login/login'));
            }
	}
    }

    /**
     *链接登陆他人账号
     */
    public function runloginUrl()
    {
        $user_id = intval(input('user_id'));//要登陆用户id
        $url_time = intval(input('url_time'));//url生成时间戳
        $secret_key = trim(input('secret_key'));//校验字符串
        $begin_time = intval(input('begin_time'));//开始时间
        $end_time = intval(input('end_time'));//结束时间
        $true_secret_key = md5($user_id.'_'.config('login_token').'_'.$url_time);
        if(isset($true_secret_key) && isset($secret_key) && $secret_key==$true_secret_key){
            //链接有效期5分钟
            if(time() - $url_time < 300) {
                //时间段判断
                if(isset($begin_time) && !empty($begin_time) && $begin_time > time()){
                    $this->error('不在有效时间段内，无法进入');
                }
                if(isset($end_time) && !empty($end_time) && $end_time < time()){
                    $this->error('不在有效时间段内，无法进入');
                }
                $user_model = new UserModel;
                if($user_model->loginById($user_id)){
                    $this->success('恭喜您，登陆成功',url('admin/Index/index'));
                }else{
                    $this->error($user_model->getError());
                }
            }else{
                $this->error('请求超时，刷新页面再试');
            }
        }else{
            $this->error('参数错误');
        }
    }
    
    /**
     * 退出登录
     */
    public function logout()
    {
        session('admin_auth_sign',null);
        session('over_time',null);
        session('last_action_time',null);
        cookie('aid', null);
        cookie('signin_token', null);
        $this->redirect('admin/Login/login');
    }
        
    /**
     * 登录显示
     */
    public function overtime_login()
    {
        $password = input('password');
        $status = model('admin/User')->overtime_login($password);
        return $status;
    }

    /**
     * 根据用户id，登录其他人账号
     */
    public function loginOther()
    {

    }
}